package com.maserb.monitor.web.analysis;

import com.maserb.monitor.dataCenter.handler.WaveRequestHandler;
import com.maserb.monitor.dataCenter.model.*;
import com.maserb.monitor.dataCenter.handler.EigenValueRequestHandler;
import com.maserb.monitor.common.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

@Controller
@RequestMapping("/realtime")
public class RealtimeController {
    @Autowired
    private EigenValueRequestHandler eigenValueRequestHandler;

    @Autowired
    private WaveRequestHandler waveRequestHandler;

    @RequestMapping("/")
    public String index() {
        return "analysis/realtime/index";
    }

    /**
     * long pull
     *
     * @param point
     * @return
     */
    @RequestMapping("/getEigenValue")
    @ResponseBody
    public DeferredResult<AjaxResult<EigenValueResponseResult>> getEigenValue(RealEigenValueModel point) {
        DeferredResult<AjaxResult<EigenValueResponseResult>> deferredResult = new DeferredResult<>();
        EigenValueRequestKey key = new EigenValueRequestKey();
        key.setDeviceCode(point.getDeviceCode());
        key.setPointCode(point.getPointCode());
        key.setEigenValueType(point.getEigenCode());

        EigenValueRequest eigenValueRequest = new EigenValueRequest(key, deferredResult);
        eigenValueRequestHandler.subscribe(eigenValueRequest);

        return deferredResult;
    }

    /**
     * long pull
     *
     * @param point
     * @return
     */
    @RequestMapping("/getWave")
    @ResponseBody
    public DeferredResult<AjaxResult<WaveResponseResult>> getWave(RealWaveModel point) {
        DeferredResult<AjaxResult<WaveResponseResult>> deferredResult = new DeferredResult<>();
        WaveRequestKey key = new WaveRequestKey();
        key.setDeviceCode(point.getDeviceCode());
        key.setPointCode(point.getPointCode());

        WaveRequest waveRequest = new WaveRequest(key, deferredResult);
        waveRequestHandler.subscribe(waveRequest);

        return deferredResult;
    }
}
